package com.lk.common;

import lombok.Data;
import java.io.Serializable;

/**
 * 统一返回类
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {
    private int code;

    private T data;

    private String message;

    private String description;

    public Result(int code, T data, String message,String description) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.description = description;
    }

    public Result(int code, T data,String description) {
        this(code,data,"",description);
    }

    public Result(int code, T data) {
        this(code,data,"","");
    }

    public Result(ErrorCode errorCode){
        this(errorCode.getCode(),null,errorCode.getMessage(),errorCode.getDescription());
    }



    /**
     * 静态方法，封装统一返回成功结果
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data){
        return new Result<>(0,data,"ok","");
    }


    /**
     * 静态方法，封装统一返回失败结果
     * @param errorCode
     * @return
     */
    public static Result error(ErrorCode errorCode){
        return new Result<>(errorCode.getCode(),null,errorCode.getMessage(),errorCode.getDescription());
    }


    /**
     *   失败
     * @param errorCode
     * @param description
     * @return
     */
    public static Result error(ErrorCode errorCode,String message,String description){
        return new Result<>(errorCode.getCode(),"",message,description);
    }


    /**
     * 失败
     * @param errorCode
     * @param description
     * @return
     */
    public static Result error(ErrorCode errorCode,String description){
        return new Result<>(errorCode.getCode(),"",errorCode.getMessage(),description);
    }

    /**
     * 失败
     * @param code
     * @param message
     * @param description
     * @return
     */
    public static Result error(int code,String message,String description){
        return new Result<>(code,"",message,description);
    }

}
